define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'product/subject/index' + location.search,
                    add_url: 'product/subject/add',
                    edit_url: 'product/subject/edit',
                    del_url: 'product/subject/del',
                    multi_url: 'product/subject/multi',
                    import_url: 'product/subject/import',
                    table: 'subject',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                search:false,
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'title', title: __('Title'), operate: 'LIKE'},
                        {field: 'number_people', title: __('Number_people'), searchList: {"15人以下":__('15人以下'),"15-30人":__('15-30人'),"30-50人":__('30-50人'),"50-100人":__('50-100人'),"100人以上":__('100人以上')}, formatter: Table.api.formatter.normal},
                        {field: 'adaptive_population', title: __('Adaptive_population'), operate: 'LIKE'},
                        {field: 'account_duration', title: __('Account_duration'), searchList: {"30分钟":__('30分钟'),"1.5小时":__('1.5小时'),"3小时":__('3小时'),"6小时":__('6小时'),"其他":__('其他')}, formatter: Table.api.formatter.normal},
                        {field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'supplier', title: __('Supplier'), operate: 'LIKE'},
                        {field: 'admin.nickname', title: __('Principal'), operate: 'LIKE'},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2')}, formatter: Table.api.formatter.status},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},

                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            // $("#c-principal_id").data("params", function (obj) {
            //     var auth_group_id = $('#c-auth_group_id').val();
            //     if(auth_group_id.length!=0)
            //     {
            //         return {custom: {type: }};
            //     }
            //
            // });
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                $(document).on('click','.types',function () {
                    var that = this
                    var type = $(that).data('type');
                    var search = $(that).data('search');
                    var val = $('#c-'+type+'_type').val();
                    var url = 'category/look?type=' +  type+'&val='+val+'&search='+search;
                    Fast.api.open(Fast.api.fixurl(url),search,{
                        callback:function(res){
                            console.log('res'+res)
                            $(that).val(res)
                            try {
                                if(typeof  callback === 'function'){
                                    callback.call(that,res)
                                }
                            }catch (e) {

                            }
                        }
                    })
                })
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
